package com.neusoft.controller;

import com.neusoft.common.Result;
import com.neusoft.pojo.User;
import com.neusoft.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping
    public Result<?> userAdd(@RequestBody User user){
        Integer s = userService.insertUser(user);
        if (1 == s) {
            return Result.success();
        }else {
            return Result.error("500","添加用户失败");
        }
    }

    @PutMapping
    public Result<?> userUpdate(@RequestBody User user){
        Integer s = userService.updateUser(user);
        if (1 == s) {
            return Result.success();
        }else {
            return Result.error("500","添加用户失败");
        }
    }


    @DeleteMapping("/{userId}")
    public Result<?> userDelete(@PathVariable String userId){
        Integer s = userService.userDelete(userId);
        if (1 == s) {
            return Result.success();
        }else {
            return Result.error("500","添加用户失败");
        }

    }

    @GetMapping("/{userId}")
    public Result<?> userByUserId(@PathVariable String userId) {
        User user = userService.userByUserId(userId);
        /*判断为空*/
        if (user == null || StringUtils.isBlank(user.getId())) {
            return Result.error("500","没有查询此用户");
        }else {
            return Result.success(user);
        }
    }

    /**
     * 查询所有用户，同时可以根据条件筛选
     * @return
     */
    @GetMapping
    public Result<?> userAll(@RequestBody User user) {
        List<User> userList = userService.userSelectAll(user);
        return Result.success(userList);
    }




}
